package com.hejjon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hejjon.core.bean.TreeNode;
import com.hejjon.core.domain.entity.FeatureCategory;
import com.hejjon.core.domain.vo.FeatureCategoryVO;
import com.hejjon.core.enumer.ViewState;
import com.hejjon.core.exception.BusinessException;
import com.hejjon.core.utils.StrUtil;
import com.hejjon.core.utils.TreeBuilder;
import com.hejjon.mapper.FeatureCategoryMapper;
import com.hejjon.service.IFeatureCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: cs
 * @date: 2025-02-26 16:27:08
 * @since: 1.0
 */
@Service
public class FeatureCategoryServiceImpl extends ServiceImpl<FeatureCategoryMapper, FeatureCategory>
        implements IFeatureCategoryService {


    @Override
    public boolean saveCategory(FeatureCategory entity) {

        String name = entity.getName();
        String text = entity.getText();

        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(text)) {
            throw new BusinessException("特征分类名称不可为空");
        }

        LambdaQueryWrapper<FeatureCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeatureCategory::getName, entity.getName())
                .or()
                .eq(FeatureCategory::getText, entity.getText());

        long count = count(wrapper);
        if (count > 0) {
            throw new BusinessException("特征分类名称已存在");
        }

        entity.setId(StrUtil.getUUID());
        entity.setHide(ViewState.SHOW.getTag());
        entity.setIcon("");
        entity.setCreateBy("admin");
        entity.setCreateTime(LocalDateTime.now());

        return super.save(entity);
    }


    @Override
    public List<TreeNode<String>> findTreeData(String kind) {

        LambdaQueryWrapper<FeatureCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FeatureCategory::getKind, kind);

        List<FeatureCategory> list = list(wrapper);
        List<TreeNode<String>> voList = new ArrayList<>();

        list.forEach(category -> {
            FeatureCategoryVO vo = new FeatureCategoryVO();
            vo.setId(category.getId());
            vo.setParentId(category.getParentId());
            vo.setName(category.getName());
            vo.setLabel(category.getText());            // element ui
            vo.setAlias(category.getAlias());
            vo.setKind(category.getKind());
            vo.setHide(category.getHide());
            vo.setLastModifyTime(category.getUpdateTime() == null ? category.getCreateTime() : category.getUpdateTime());
            vo.setLastModifyUser(category.getUpdateBy() == null ? category.getCreateBy() : category.getUpdateBy());

            voList.add(vo);
        });

        TreeBuilder<String> treeBuilder = new TreeBuilder<>();

        return treeBuilder.buildTree(voList);
    }

}
